//
//  BaseViewController.m
//  HOrangiOS
//
//  Created by YOrange on 2018/4/24.
//  Copyright © 2018年 YOrange. All rights reserved.
//

#import "BaseViewController.h"
@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}


-(void)initRightButtonItem:(NSString*)btnContent withEdgeInset:(UIEdgeInsets)edgeInsets  {
    UIButton *right = [UIButton buttonWithType:UIButtonTypeSystem];
    [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    right.frame =CGRectMake(0, 0, 60, 60);
    [right.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [right setTitle:btnContent forState:UIControlStateNormal];
    if (@available(iOS 9.0, *)) {
        [right setTitle:@"12" forState:UIControlStateFocused];
    } else {
        [right setTitle:@"12" forState:UIControlStateNormal];
    }
    [right setTitleEdgeInsets:edgeInsets];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)initRightButtonImgItem:(UIImage*)img withEdgeInsets:(UIEdgeInsets)edgeInsets{
    UIButton *right = [[UIButton alloc]init];
    right.frame =CGRectMake(0, 0, 44, 44);
    [right setImage:img forState:UIControlStateNormal];
    [right setImageEdgeInsets:edgeInsets];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)initLeftButtonItem{
    UIButton * btnLeft=[[UIButton alloc]init];
    btnLeft.frame=CGRectMake(0, 0, 60, 54);
    [btnLeft setImage:[UIImage imageNamed:@"goBack.png"] forState:UIControlStateNormal];//设置你需要加载的返回按钮图片
    [btnLeft addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [btnLeft setImageEdgeInsets:UIEdgeInsetsMake(0, -38, 0, 0)];
    btnLeft.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:btnLeft];
    
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)hideLeftButtonItem{
    UIButton *left = [[UIButton alloc]init];
    left.frame =CGRectMake(0, 0, 60, 44);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:left];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        negativeSpacer.width = -10;
    } else {
        negativeSpacer.width = 0;
    }
    [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,self.navigationItem.leftBarButtonItem, nil] animated:YES];
}

-(void)topMoreClick{
    
}

/*********************MBProgressHUD************************************/

-(MBProgressHUD *)mbProgress{
    if (_mbProgress == nil) {
        _mbProgress=[[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:_mbProgress];
        _mbProgress.removeFromSuperViewOnHide=YES;
    }
    return _mbProgress;
}

-(void)mbShowLoadingText{
    [self mbShowLoadingText:nil];
}


-(void)mbShowLoadingText:(NSString*)loadingText{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.mbProgress.label.text = loadingText;
        self.mbProgress.mode = MBProgressHUDModeIndeterminate;
        [self.mbProgress showAnimated:YES];
    });
}


-(void)mbShowToast:(NSString*)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.mbProgress.label.text=str;
        self.mbProgress.mode = MBProgressHUDModeText;
        [self.mbProgress showAnimated:YES];
        [self performSelector:@selector(mbDismiss) withObject:nil afterDelay:1];
    });
    
}



-(void)mbDismiss{
    [self.mbProgress hideAnimated:YES];
}


///错误提示
-(void)mbShowError:(NSError*)error{
    NSString * info=nil;
    if ([error.userInfo.allKeys containsObject:@"NSLocalizedDescription"]) {
        info=[error.userInfo objectForKey:@"NSLocalizedDescription"];
    }else {
        info=error.domain;
    }
    if([info isEqualToString:@"NSCocoaErrorDomain"]){
        info = @"找不到服务器!";
    }
    [self mbShowToast:info];
}

/*********************返回************************************/
-(void)back:(int)indexVc{
    int index = (int)[[self.navigationController viewControllers]indexOfObject:self];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(index - indexVc)] animated:YES];
}



-(void)dealloc{
    NSLog(@"%s deall",__FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
